import requests
import os
import re
import shutil
import pymongo

client = pymongo.MongoClient('localhost', 27017)
db = client.get_database("率土之滨")
collections = db.get_collection("武将库")

if not os.path.exists(f'static/率土之滨/武将库'):
    os.makedirs('static/率土之滨/武将库')
else:
    shutil.rmtree('static/率土之滨/武将库')
    os.makedirs('static/率土之滨/武将库')

url = 'https://g0.gph.netease.com/ngsocial/community/stzb/cfg/hero_extra.json?gameid=g10'
datas = requests.get(url)
collections.insert_many(datas.json())
client.close()
for data in datas.json():
    # print(data['name'], data['iconId'])
    # 'https://g0.gph.netease.com/ngsocial/community/stzb/cn/cards/cut/card_small_100352.jpg?gameid=g10'
    # 'https://g0.gph.netease.com/ngsocial/community/stzb/cn/cards/cut/card_small_100354.jpg?gameid=g10'
    img_url = f'https://g0.gph.netease.com/ngsocial/community/stzb/cn/cards/cut/card_small_{data['iconId']}.jpg?gameid=g10'
    img_response = requests.get(img_url)
    img_content = img_response.content
    with open(f'static/率土之滨/武将库/{data["name"]}.png', 'wb') as f:
        f.write(img_content)

    break
